package com.daikuan.fffkuaiban.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jarhead.common.commonutils.DensityUtil;
import com.daikuan.fffkuaiban.R;
import com.daikuan.fffkuaiban.bmobBean.ListConfig;
import com.daikuan.fffkuaiban.utils.ImageLoader;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by jiming1 on 2017/7/1.
 */
public class YouxuanDaikuanListAdapter extends BaseAdapter {
    private Context context;
    private List<ListConfig> listConfigs;

    public YouxuanDaikuanListAdapter(Context context, List<ListConfig> listConfigs) {
        this.context = context;
        this.listConfigs = listConfigs;
    }

    @Override
    public int getCount() {
        return listConfigs.size();
    }

    @Override
    public ListConfig getItem(int i) {
        return listConfigs.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        BaseViewHolder viewHolder = null;
        final ListConfig hotChannel = getItem(i);
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_youxuan, viewGroup, false);
            viewHolder = new BaseViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (BaseViewHolder) view.getTag();
        }

        viewHolder.tvName.setText(hotChannel.channel_name);
        viewHolder.tvQuotaRange.setText(hotChannel.money_range);
//        viewHolder.tvSuccessRate.setText(hotChannel.successRate);
        viewHolder.llContain.removeAllViews();
        viewHolder.llContain2.removeAllViews();
        viewHolder.llContain2.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(hotChannel.tags)) {
            String[] tags = hotChannel.tags.split("，");
            if (tags != null && tags.length > 0) {

                for (int i1 = 0; i1 < tags.length; i1++) {
                    String tag = tags[i1];
//                    String tag ="身份证";
                    TextView textView = new TextView(context);
                    textView.setText(tag);
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12f);
                    textView.setBackground(ContextCompat.getDrawable(context, R.drawable.label_drawable));
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    int margin = DensityUtil.dip2px(context, 3);
                    params.setMargins(margin, 0, margin, 0);
                    textView.setLayoutParams(params);
                    int padding = DensityUtil.dip2px(context, 5);
                    textView.setPadding(padding, margin, padding, margin);
                    textView.setSingleLine();
                    if (i1 < 3) {
                        viewHolder.llContain.addView(textView);
                    } else {
                        viewHolder.llContain2.setVisibility(View.VISIBLE);
                        viewHolder.llContain2.addView(textView);
                    }
                }
            }
        }
        if (hotChannel.channel_logo == null) {
            ImageLoader.loadImage(context, "", viewHolder.ivImage);
        } else {
            ImageLoader.loadImage(context, hotChannel.channel_logo.url, viewHolder.ivImage);
        }


        return view;
    }

    public void update(List<ListConfig> beans) {
        this.listConfigs.clear();
        this.listConfigs.addAll(beans);
        notifyDataSetChanged();
    }

    public void add(List<ListConfig> beans) {
        this.listConfigs.addAll(beans);
        notifyDataSetChanged();
    }

    class BaseViewHolder {
        @Bind(R.id.ivImage)
        ImageView ivImage;
        @Bind(R.id.tvName)
        TextView tvName;
        @Bind(R.id.tvQuotaRange)
        TextView tvQuotaRange;
        @Bind(R.id.tvSuccessRate)
        TextView tvSuccessRate;
        @Bind(R.id.llContain)
        LinearLayout llContain;
        @Bind(R.id.llContain2)
        LinearLayout llContain2;

        public BaseViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


}
